﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;

namespace CuaHangHoa
{
    public partial class Them_ncc : Form
    {
        public Them_ncc()
        {
            InitializeComponent();
        }


        public delegate void DoEvent();
        public event DoEvent RefreshDgv;

        private void ThemNCC()
        {
            try
            {
                Nhacungcap_DTO nccDTO = new Nhacungcap_DTO();
                nccDTO.TenNCC = txt_tenncc.Text.Trim();
                nccDTO.SDT = int.Parse(txt_SDT.Text.Trim());
                nccDTO.Email = txt_Email.Text.Trim();
                nccDTO.Diachi = txt_diachi.Text.Trim();
                nccDTO.Fax = txt_Fax.Text.Trim();
                nccDTO.Website = txt_website.Text.Trim();
                nccDTO.Taikhoan = txt_TKNH.Text.Trim();
                nccDTO.TinhTrang = txt_tinhtrang.Text.Trim();
                nccDTO.GhiChu = txt_ghichu.Text.Trim();
                int kq;
                Nhacungcap_BUS bus = new Nhacungcap_BUS();
                kq = bus.ThemNhaCungCap(nccDTO);

                if (kq == 1)
                {
                    this.RefreshDgv();
                    MessageBox.Show("Thêm thành công");
                }
                else
                {
                    MessageBox.Show("Thêm thất bại");
                }
                txt_tenncc.Focus();
            }
            catch (System.Exception ex)
            {
                errorProvider1.SetError(txt_tenncc, "Nhà cung cấp này đã tồn tại");
                txt_tenncc.Focus();
            }

        }

       private void txt_tenncc_KeyDown_1(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txt_SDT.Focus();
            }
        }

        private void txt_diachi_KeyDown_1(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txt_Fax.Focus();
            }
        }

        private void txt_SDT_KeyDown_1(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txt_Email.Focus();
            }
        }

        private void txt_website_KeyDown_1(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txt_TKNH.Focus();
            }
        }
        private void txt_Fax_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txt_website.Focus();
            }
        }
        private void txt_Email_KeyDown_1(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txt_diachi.Focus();
            }
        }

        private void txt_TKNH_KeyDown_1(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txt_tinhtrang.Focus();
            }
        }
        private void txt_tinhtrang_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txt_ghichu.Focus();
            }
        }

        private void txt_ghichu_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                ThemNCC();
                errorProvider1.Clear();
            }
        }

        private void btThem_Click_1(object sender, EventArgs e)
        {
            ThemNCC();
            errorProvider1.Clear();
        }

        private void bt_new_Click_1(object sender, EventArgs e)
        {
            txt_tenncc.Text = "";
            txt_SDT.Text = "";
            txt_Email.Text = "";
            txt_diachi.Text = "";
            txt_Fax.Text = "";
            txt_website.Text = "";
            txt_TKNH.Text = "";
            txt_tinhtrang.Text = "";
            txt_ghichu.Text = "";
            txt_tenncc.Focus();
        }

        private void btThoat_Click_1(object sender, EventArgs e)
        {
            Close();
        }

        private void txt_SDT_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
                errorProvider1.SetError(txt_SDT, "Chi được nhập số");
            }
        }

        private void Them_ncc_Load(object sender, EventArgs e)
        {

        }

        private void Them_ncc_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult ok;
            ok = MessageBox.Show("Bạn có muốn thoát ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (ok == DialogResult.No)
            {
                e.Cancel = true;
            }
        }

        
       

      

      

        

    }
}
